home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 223_01 / itod.c < prev    next >
Text File  |  1980-01-01  |  896b  |  26 lines

  1. #include stdio.h
  2. /*
  3. ** itod -- convert nbr to signed decimal string of width sz
  4. **         right adjusted, blank filled; returns str
  5. **
  6. **        if sz > 0 terminate with null byte
  7. **        if sz = 0 find end of string
  8. **        if sz < 0 use last byte for data
  9. */
  10.    static char sgn;
  11.  
  12. itod(nbr, str, sz)  int nbr;  char str[];  int sz;  {
  13.   if(nbr<0) {nbr = -nbr; sgn='-';}
  14.   else sgn=' ';
  15.   if(sz>0) str[--sz]=NULL;
  16.   else if(sz<0) sz = -sz;
  17.   else while(str[sz]!=NULL) ++sz;
  18.   while(sz) {
  19.     str[--sz]=(nbr%10+'0');
  20.     if((nbr=nbr/10)==0) break;
  21.     }
  22.   if(sz) str[--sz]=sgn;
  23.   while(sz>0) str[--sz]=' ';
  24.   return str;
  25.   }
  26.